<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Plugin {

	public function start() {
		global $request;

		$action = $request->get( 'p' );

		$id = $request->id;

		switch( $action ) {
			case 'installexe':
				$this->install_exe();
				break;
			case 'remove':
				$this->remove();
				break;
			default:
				$this->show( $action );
		}
	}

	private function install_exe() {
		$path = getGP( 'Path','G' );
		include( XKLOG_ROOT . $path . 'install.php' );
		cache_plugin();
		show_admin_message( '插件安装成功', 'admin_plugin.php?action=Install');
		exit;
	}

	private function remove() {
		$path = getGP( 'Path','G' );
		include( XKLOG_ROOT . $path . 'remove.php' );
		cache_plugin();
		show_admin_message( '插件卸载成功', 'admin_plugin.php?action=List');
		exit;
	}

	private function show( $action, $message = '', $type = '' ) {
		global $cache;
		if( $message != '' ) {
			$message = '<p class="' . $type . '">' . $message . '</p>';
		}
		@header("content-type: text/html; charset=utf-8");
?>
<div class = "admin_panel">
	<div class = "admin_title">插件管理</div>
	<div class = "admin_content">
	<?php
	switch( $action ) {
		case 'set':
			$this->plugin_set();
			break;
		default:
			$this->plugin_list();
	}
	?>
	</div>
</div>
<?php
	}

	private function plugin_list( $method = 'list' ) {
		global $cache;
?>
<div style="padding:5px;margin:2px;">
	<table style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#bbbbbb">
		<tr align="center" height="22" bgcolor="#ffffff">
			<td width="3%"></td>
			<td width="4%">序号</td>
			<td width="15%">插件名称</td>
			<td width="10%">插件作者</td>
			<td width="10%">插件版本</td>
			<td width="45%">插件描述</td>
			<td width="13%">管理操作</td>
		</tr>
		<?php
		$plugin_root = APP_ROOT . 'plugin/';
		$result = read_dir($plugin_root);
		$plugins = array();
		foreach ($result as $each_dir) {
			$each_dir = str_replace($plugin_root, '', $each_dir);
			if( !$this->is_legal( $each_dir ) ) continue;
			$plugins[$each_dir]['directory'] = $each_dir;
			$plugins[$each_dir]['path'] = 'plugin/' . $each_dir . '/';
			$infofile = APP_ROOT . $plugins[$each_dir]['path'].'info.xml';
			$info = parse_xml_info( $infofile,'plugin' );
			$plugins[$each_dir] = array_merge( $plugins[$each_dir], $info );
		}
		$i = 1;
		foreach ($plugins as $plugin) {
			/*if( $method == 'list' ) {
				if( !check_install( $plugin['path'] ) ) continue;
			}else{
				if( check_install( $plugin['path'] ) ) continue;
			}*/
		?>
		<tr align="center" height="20" bgcolor="#ffffff">
			<td ></td>
			<td><?php echo $i ?></td>
			<td><?php echo $plugin['name'] ?></td>
			<td>
			<?php
			if( $plugin['homepage'] == '' ) {
				echo $plugin['author'];
			}else{ ?>
				<a href="<?php echo $plugin['homepage'] ?>" target="_blank"><?php echo $plugin['author'] ?></a>
			<?php } ?>
			</td>
			<td><?php echo $plugin['version'] ?></td>
			<td><?php echo $plugin['intro'] ?></td>
			<td>
			<?php if( $method == 'list' ) { ?>
				<span class="Control">
				<img src="include/edt.gif" width="16" height="16" /><a href="admin_plugin.php?action=set&Path=<?php echo $plugin['path'] ?>">配置</a>&nbsp; &nbsp;
				<img src="include/del.gif" width="16" height="16" /><a href="javascript:if(confirm('确实要卸载吗?'))location='admin_plugin.php?action=Remove&Path=<?php echo $plugin['path'] ?>'">卸载</a>
				</span>
			<?php }else{ ?>
				<span class="Control">
				<img src="include/edt.gif" width="16" height="16" /><a href="admin_plugin.php?action=InstallExe&Path=<?php echo $plugin['path'] ?>">安装</a>
				</span>
			<?php } ?>
			</td>
		</tr>
		<?php
			$i ++;
		}
		?>
	</table>
</div>
<div class="main_button"></div>
<?php
}
	static public function is_legal( $p ) {
	
	}

	private function plugin_set() {
		$path = getGP( 'Path','G' );
		include( APP_ROOT . $path . 'set.php' );
	}

}
?>
